package nacos.example.config;

import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.cloud.nacos.NacosPropertySourceRepository;
import com.alibaba.cloud.nacos.client.NacosPropertySource;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.Objects;
import java.util.Properties;

//@Configuration(proxyBeanMethods = false)
public class CustomNaocsConfiguration {

    @Resource
    CustomNacosConfig customNacosConfig;


    @PostConstruct
    public void init() {
        if (Objects.nonNull(customNacosConfig)) {
            Date date = new Date();

            NacosPropertySourceRepository.collectNacosPropertySource(new NacosPropertySource(null, customNacosConfig.getGroup(), customNacosConfig.getDataId(),date, customNacosConfig.isRefreshEnabled()));
            if (!CollectionUtils.isEmpty(customNacosConfig.getExtensionConfigs())) {
                customNacosConfig.getExtensionConfigs().forEach(config -> NacosPropertySourceRepository.collectNacosPropertySource(new NacosPropertySource(null, config.getGroup(), config.getDataId(), date, config.isRefresh())));
            }
        }
    }

    //@Configuration
    public static class CustomNacosConfigProperties extends NacosConfigProperties {
        @Override
        public Properties assembleConfigServiceProperties() {
            Properties properties = super.assembleConfigServiceProperties();
            System.out.println(properties);
            return properties;
        }
    }
}
